/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.controle;

import br.com.ecommercer.bean.Cliente;
import br.com.ecommercer.bean.Perfil;
import br.com.ecommercer.bean.Pessoa;
import br.com.ecommercer.bean.Usuario;
import br.com.ecommercer.dao.ClienteDao;
import br.com.ecommercer.dao.ClienteDaoImp;
import br.com.ecommercer.dao.PessoaDao;
import br.com.ecommercer.dao.UsuarioDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tainara
 */
@WebServlet(name = "ClienteServlet", urlPatterns = {"/ClienteServlet"})
public class ClienteServlet extends HttpServlet {


        private HttpServletRequest request;
        private HttpServletResponse response;
        private RequestDispatcher rd;
        private Cliente cliente;
        private ClienteDao clienteDao;
        private Pessoa pessoa;
        private PessoaDao pessoaDao;
        private Usuario usuario;
        private UsuarioDao usuarioDao;

        protected void processRequest() throws ServletException, IOException {
            String cmd = request.getParameter("cmd");
            switch (cmd) {
                case "salvar":
                    salvar();
                    break;
                case "alterar":
                    alterar();
                    break;
                case "procurar":
                    procurarPorId();
                    break;

            }
            rd.forward(request, response);
        }

        private void carregaCliente() {
            cliente = new Cliente();
            Perfil perfil = new Perfil();
            usuario = new Usuario();
            String id = request.getParameter("id");
            if (id != null) {
                cliente.setId(Integer.parseInt(id));
            }
            cliente.setCpf(request.getParameter("cpf"));
            cliente.setRg(request.getParameter("rg"));
            cliente.setNome(request.getParameter("nome"));
            cliente.setEmail(request.getParameter("email"));
            cliente.setCelular(request.getParameter("celular"));
            cliente.setTelefone(request.getParameter("telefone"));
            
            usuario.setLogin(request.getParameter("login"));
            usuario.setSenha(request.getParameter("senha"));
            
            perfil.setId(Integer.parseInt(request.getParameter("idPerfil")));
            
            usuario.setPerfil(perfil);
            cliente.setUsuario(usuario);
                    
            
            
        }

        private void salvar() {
            carregaCliente();
            clienteDao = new ClienteDaoImp();
            try {
                clienteDao.salvar(cliente);
                request.setAttribute("msg", "cliente salvo com sucesso!");
            } catch (Exception e) {
                request.setAttribute("msg", "erro ao salvar cliente!");
            }
            rd = request.getRequestDispatcher("index.jsp");
        }

        private void procurarPorId() {
            clienteDao = new ClienteDaoImp();
            Integer id = Integer.parseInt(request.getParameter("id"));
            try {
                cliente = (Cliente) clienteDao.procurar(id);
                request.setAttribute("cliente", cliente);
                rd = request.getRequestDispatcher("alt_endereco.jsp");
            } catch (Exception e) {
            }
        }

        private void alterar() {
            clienteDao = new ClienteDaoImp();
            carregaCliente();
            try {
                clienteDao.alterar(cliente);
                request.setAttribute("msg", "cliente alterado com sucesso!");
                rd = request.getRequestDispatcher("pesq_endereco.jsp");
            } catch (Exception e) {
                request.setAttribute("msg", "erro ao alterar cliente!");
            }
        }

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.request = request;
            this.response = response;
            processRequest();
        }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.request = request;
            this.response = response;
            processRequest();
        }

        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    }
    